
var newer = newer || {};
newer.gd = newer.gd || {};

newer.TitleDiv = function(o) {
	this.domNode = o.domNode;
	
	this.init();
};

newer.TitleDiv.prototype = {
	init: function() {
		this.domNode.html("");
		
		this.domNode.removeClass("nw-event-listener").addClass("nw-event-listener");
		this.domNode.off("nw-event").on("nw-event", $.proxy(this.nweHandler,this));
		
		this.refresh();
	},
	
	refresh: function() {
		this.layout();
	},
	
	nweHandler: function(e,c) {
		if (c.type == newer.EventHelper.PAGE_TYPE) {
			if (c.fromStatus.ptype != c.toStatus.ptype) {
				this.refresh();
			} else if (c.fromStatus.ptype == newer.UriHelper.PEOPLE_TYPE) {
				$("#name-overview-div").html(newer.gd.people.name);
				$("#add-friend-div").html("").addClass("title-div-button").hide();
				this.addAddFriendButton();
			} else if (c.fromStatus.ptype == newer.UriHelper.CIRCLE_TYPE) {
				$("#name-overview-div").html(newer.gd.circle.name);
				$("#join-circle-div").html("").addClass("title-div-button").hide();
				this.addJoinCircleButton();
			}
		} else if (c.type == newer.EventHelper.AUTH_TYPE) {
			this.updateAddFriendButton();
			this.updateJoinCircleButton();
		} else if (c.type == newer.EventHelper.DATA_TYPE) {
			if (c.props.type == "member") {
				this.updateJoinCircleButton();
			} else if (c.props.type == "friend") {
				this.updateAddFriendButton();
			}
		}
	},
	
	layout: function() {
		if(newer.gd.pstatus.ptype == newer.UriHelper.PEOPLE_TYPE) {
			this.domNode.html("<div id=\"name-overview-div\" class=\"title-overview-div-pos\"></div><div id=\"add-friend-div\" class=\"title-overview-div-pos\"></div>");
			$("#name-overview-div").html(newer.gd.people.name);
			$("#add-friend-div").html("").addClass("title-div-button").addClass("link-like-element").hide();
			this.updateAddFriendButton();
		} else if(newer.gd.pstatus.ptype == newer.UriHelper.CIRCLE_TYPE) {
			this.domNode.html("<div id=\"name-overview-div\" class=\"title-overview-div-pos\"></div><div id=\"join-circle-div\" class=\"title-overview-div-pos\"></div>");
			$("#name-overview-div").html(newer.gd.circle.name);
			$("#join-circle-div").html("").addClass("title-div-button").addClass("link-like-element").hide();
			this.updateJoinCircleButton();
		}		
	},
	
	updateAddFriendButton: function() {
		$("#add-friend-div").off("click");
		this.addAddFriendButton();
	},
	
	addAddFriendButton: function() {
		var self = this;
		$.post("/Friends/currentUserHasFriend", {"u":newer.gd.people}, function(result) {
			if(result.state == "no friend") {
				$("#add-friend-div").show();
				$("#add-friend-div").html("Add friend").addClass("title-overview-div-pos");
				$("#add-friend-div").off("mouseenter");
				$("#add-friend-div").off("mouseout");
				$("#add-friend-div").off("click");
				$("#add-friend-div").on("click", function() {
					$.post("/Friends/currentUserAddFriend", {"u":newer.gd.people}, function(result) {
						if (result == true) {
							$(".nw-event-listener").trigger('nw-event', {
								type:newer.EventHelper.DATA_TYPE, 
								props:{
									operation:newer.EventHelper.DATA_OP_ADD, 
									type:"friend",
									data:[newer.gd.user.id,newer.gd.people.id]
								}
							});
						}
					});
				});	
			} else if(result.state == "friend") {
				$("#add-friend-div").show();
				$("#add-friend-div").html("Friend").addClass("title-overview-div-pos");
				$("#add-friend-div").off("click");
				$("#add-friend-div").on("click", function() {
					$.post("/Friends/currentUserDeleteFriend", {"u":newer.gd.people}, function(result){
						if (result == true) {
							$(".nw-event-listener").trigger('nw-event', {
								type:newer.EventHelper.DATA_TYPE, 
								props:{
									operation:newer.EventHelper.DATA_OP_DELETE, 
									type:"friend",
									data:[newer.gd.user.id,newer.gd.people.id]
								}
							});
						}
					}, 'json');
				});
				$("#add-friend-div").on("mouseenter", function() {
					$("#add-friend-div").html("delete friend");
				});
				$("#add-friend-div").on("mouseout", function() {
					$("#add-friend-div").html("Friend");
				});
			} else {
				$("#add-friend-div").hide();
			}
		});	
	},
	
	updateJoinCircleButton: function() {
		$("#join-circle-div").off("click");
		this.addJoinCircleButton();
	},
	
	addJoinCircleButton: function() {
		var self = this;
		if(newer.gd.pstatus.ptype != newer.UriHelper.CIRCLE_TYPE) { return; }
		$.post("/Circles/currentUserInCircle", {"cid":newer.gd.circle.id}, function(result) {
			if(result.state == "not in circle") {
				$("#join-circle-div").show();
				$("#join-circle-div").html("Join circle").addClass("title-overview-div-pos");
				$("#join-circle-div").off("mouseenter");
				$("#join-circle-div").off("mouseout");
				$("#join-circle-div").off("click");
				$("#join-circle-div").on("click", function() {
					$.post("/Circles/addMember", {"cid":newer.gd.circle.id}, function(result) {
						if (result == true) {
							$(".nw-event-listener").trigger('nw-event', {
								type:newer.EventHelper.DATA_TYPE, 
								props:{
									operation:newer.EventHelper.DATA_OP_ADD, 
									type:"member",
									data:[newer.gd.circle.id,newer.gd.user.id]
								}
							});
						}
					}, 'json');
				});	
			} else if(result.state == "in circle") {
				$("#join-circle-div").show();
				$("#join-circle-div").html("Member").addClass("title-overview-div-pos");
				$("#join-circle-div").off("click");
				$("#join-circle-div").on("click", function() {
					$.post("/Circles/currentUserLeaveCircle", {"cid":newer.gd.circle.id}, function(result) {
						if (result == true) {
							$(".nw-event-listener").trigger('nw-event', {
								type:newer.EventHelper.DATA_TYPE, 
								props:{
									operation:newer.EventHelper.DATA_OP_DELETE, 
									type:"member",
									data:[newer.gd.circle.id,newer.gd.user.id]
								}
							});
						}
					}, 'json');
				});
				$("#join-circle-div").on("mouseenter", function() {
					$("#join-circle-div").html("Leave circle");
				});
				$("#join-circle-div").on("mouseout", function() {
					$("#join-circle-div").html("Member");
				});
			} else {
				$("#join-circle-div").hide();
			}
		});	
	}
};
